﻿using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using System.Text.Json;
using XieCan.Core.Config;

namespace XieCan.Core.JWT
{
  public class JWTHelper
  {
    public static string CreateToken(IList<Claim> claims, DateTime expires)
    {
      var tokenDescriptor = new SecurityTokenDescriptor
      {
        Issuer = AppSettingsHelper.GetValue("JWT:Issuer"),
        Audience = AppSettingsHelper.GetValue("JWT:Audience"),
        Subject = new ClaimsIdentity(claims),
        Expires = expires,
        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppSettingsHelper.GetValue("JWT:SecretKey"))), SecurityAlgorithms.HmacSha256Signature)
      };
      var tokenHandler = new JwtSecurityTokenHandler();
      var token = tokenHandler.CreateToken(tokenDescriptor);
      var tokenStr = tokenHandler.WriteToken(token);
      return tokenStr;
    }
  }
}
